package com.freak.wanandroid.model.home;

import com.freak.httpmanager.callback.SubscriberCallBack;
import com.freak.wanandroid.base.repository.BaseRepository;
import com.freak.wanandroid.bean.BannerBean;
import com.freak.wanandroid.bean.SystemClassChildrenEntity;
import com.freak.wanandroid.bean.SystemClassifyBean;
import com.freak.wanandroid.model.search.db.RealmHelper;
import com.freak.wanandroid.net.HttpResultFunc;

import java.util.List;

import io.reactivex.rxjava3.core.Observable;


public class HomepageRepository extends BaseRepository {
    private RealmHelper mRealmHelper = new RealmHelper();

    public List<SystemClassifyBean> getSystemClassify() {
        return mRealmHelper.getSystemClassify();
    }

    public List<SystemClassChildrenEntity> getSpecialSubscriptionInfo() {
        return mRealmHelper.getSpecialSubscriptionInfo();
    }

    public List<SystemClassChildrenEntity> getSpecialInfo(int parentId) {
        return mRealmHelper.getSpecialInfo(parentId);
    }

    public boolean saveSpecialSubscription(List<SystemClassifyBean> list, String defaultSubscription) {
        for (SystemClassifyBean systemClassifyBean : list) {
            List<SystemClassChildrenEntity> children = systemClassifyBean.getChildren();
            for (SystemClassChildrenEntity child : children) {
                if (defaultSubscription.equals(child.getName())) {
                    child.setSubscription(1);
                }
            }
        }
        return mRealmHelper.saveSpecialSubscription(list);
    }

    public void updateSpecialSubscriptionInfo(SystemClassChildrenEntity classChildrenEntity,int subscription) {
        mRealmHelper.updateSpecialSubscriptionInfo(classChildrenEntity,subscription);
    }

    public void getSystem(SubscriberCallBack<List<SystemClassifyBean>> observer) {
        Observable observable = mApiService.getSystemClassify().map(new HttpResultFunc<List<SystemClassifyBean>>());
        addSubscription(observable, observer);
    }

    /**
     * 获取轮播图
     *
     * @param observer
     */
    public void getBanner(SubscriberCallBack<List<BannerBean>> observer) {
        Observable observable = mApiService.getBanner().map(new HttpResultFunc<List<BannerBean>>());
        addSubscription(observable, observer);
    }
}
